package com.yun.controller;



import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.entity.*;
import com.yun.service.BussinessService;
import com.yun.utils.MyConstants;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 业务表(Bussiness)表控制层
 *
 * @author makejava
 * @since 2022-01-20 14:51:30
 */
@RestController
@RequestMapping("bussiness")
public class BussinessController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private BussinessService bussinessService;


    /**
     * 跳转到业务管理页面
     */
    @RequestMapping("/toBussiness")
    public ModelAndView toDept() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("bussiness/showBussiness");
        return modelAndView;
    }
    /**
     * 分页查询所有数据
     */
    @RequestMapping("selectAll")
    public ReturnBean selectAll(Long page, Long limit, Bussiness bussiness) {
        List<Bussiness> userVoList = this.bussinessService.findAll(page, limit, bussiness);
        return super.success(userVoList, bussinessService.getCount(bussiness));
    }

    /**
     * 新增业务
     * @return 新增结果
     */
    @PostMapping("insertBussiness")
    public ReturnBean insert(@RequestBody Bussiness bussiness, HttpSession session) {
        bussiness.setGmtCreate(new Date());
        boolean save = this.bussinessService.save(bussiness);
        if (save) {
            return super.success(bussiness);
        } else {
            return super.fail(bussiness);
        }
    }
    /**
     * 修改类型
     */
    @PutMapping("updateBussiness")
    public ReturnBean update(@RequestBody Bussiness bussiness, HttpSession session) {
        bussiness.setGmtModified(new Date());
        boolean update = this.bussinessService.updateById(bussiness);
        if (update ) {
            return super.success(bussiness);
        } else {
            return super.fail(bussiness);
        }
    }
    /**
     * 删除数据
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("deleteBussiness")
    public ReturnBean delete(@RequestParam(value = "idList[]", required = false) List<Long> idList) {
        boolean removeByIds = this.bussinessService.removeByIds(idList);
        if (removeByIds) {
            return super.success(null);
        } else {
            return super.fail(null);
        }
    }

    @RequestMapping("findOne")
    public ReturnBean findOne(String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bname", name);
        Bussiness one = bussinessService.getOne(queryWrapper);
        if (one == null) {
            //不存在
            return success(one, 1L);
        } else {
            return fail(null);
        }
    }
}

